package cn.doit.crawler.demo

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements

/**
 * @author: 余辉
 * @blog:   https://blog.csdn.net/silentwolfyh
 * @create: 2019/11/12
 * @description:  Jsou工具解析页面
 **/
object JsoupDemo {

  def main(args: Array[String]): Unit = {

    val html_doc =
      """
      <html>
        <head>
          <title>The Dormouse's story title</title>
        </head>
      <body>

      <p class="title"><b>The Dormouse's story</b></p>

      <p class="story">
        Once upon a time there were three little sisters; and their names were
        <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
        <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
        <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
        and they lived at the bottom of a well.
      </p>

      <p class="story">...</p>
      </body>
      </html>
      """
    // 1、解析html的页面信息
    val doc: Document = Jsoup.parse(html_doc)
    // 2、获取标签 doc.title
    val title: String = doc.title
    println(title)

    // 3、获取所有链接 doc.select("a[href]")
    val links: Elements = doc.select("a[href]")
    import scala.collection.JavaConversions._
    for (link <- links) {
      // 3-1、获取具体url link.attr("href")
      System.out.println("\nlink : " + link.attr("href"))
      // 3-2、获取具体url的内容 link.text
      System.out.println("text : " + link.text)
    }

    // 4、获取class为title子节点 doc.getElementsByClass("title")
    val elements: Elements = doc.getElementsByClass("title")
    println(elements.text())
  }
}
